package com.yeyoo.websocket.handler;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

/**
 * WebSocket拦截器
 * 
 * @author jinyifeng5969@163.com
 * @since 2018年12月22日 下午3:55:16
 * @version v1.0.0
 *
 */
public class YeyooWebSocketInterceptor extends HttpSessionHandshakeInterceptor {
    
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler,
                               Exception ex) {
        super.afterHandshake(request, response, handler, ex);
    }
    
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler,
                                   Map<String, Object> attributes) throws Exception {
        /*if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servlet = (ServletServerHttpRequest) request;
            HttpSession session = servlet.getServletRequest().getSession(false);
            if (null != session) {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                attributes.put(Constant.$USERNAME, ObjectUtils.defaultIfNull(authentication.getName(), "default-system"));
            }
        }*/
        return super.beforeHandshake(request, response, handler, attributes);
    }
    
}